package com.niit.ssm.dao.impl;

import com.niit.ssm.dao.StudentInfo20Dao;
import com.niit.ssm.entity.StudentInfo20;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class StudentInfo20DaoImpl implements StudentInfo20Dao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public StudentInfo20 queryById(String id) {
        return jdbcTemplate.queryForObject("select * from student_info_20 where student_id = ?", new BeanPropertyRowMapper<>(StudentInfo20.class),id);
    }

    @Override
    public long count(StudentInfo20 studentInfo20) {
        return 0;
    }

    @Override
    public int insert(StudentInfo20 studentInfo20) {
        return 0;
    }

    @Override
    public int update(StudentInfo20 studentInfo20) {
        return 0;
    }

    @Override
    public int deleteById() {
        return 0;
    }

    @Override
    public int queryCount() {
        return jdbcTemplate.queryForObject("select count(*) from student_info_20",Integer.class);
    }

    @Override
    public List<StudentInfo20> queryByName(StudentInfo20 info) {
        return jdbcTemplate.query("select * from student_info_20 where chinese_name like ?"
                ,new BeanPropertyRowMapper<>(StudentInfo20.class)
                ,"%"+info.getChineseName()+"%");
    }

    @Override
    public StudentInfo20 queryOne(StudentInfo20 studentInfo20) {
        String sql ="select * from student_info_20 where 1=1";
        if (!studentInfo20.getChineseName().isEmpty()) {
            sql += " and chinese_name = '"+studentInfo20.getChineseName()+"'";
        }
        if (!studentInfo20.getStudentId().isEmpty()) {
            sql += " and student_id = '"+ studentInfo20.getStudentId()+"'";
        }
        StudentInfo20 studentInfo201 = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(StudentInfo20.class));
        return studentInfo201;
    }
}
